<template>
    <div>
        <h1>login page</h1>
        <div style="margin: 30px;"></div>
        <el-form label-position="left" label-width="110px" :model="formData">
            <el-form-item label="用户名：admin">
                <el-input v-model="formData.username" :autofocus="true"></el-input>
            </el-form-item>
            <el-form-item label="密码：admin" >
                <el-input v-model="formData.password" @keyup.enter.native="doSubmit"></el-input>
            </el-form-item>
        </el-form>
        <el-button type="primary" @click="doSubmit">login</el-button>
    </div>
</template>


<script>
    import {zuulHost,uamServer,clientID,authHost,tokenUri,getToken,initDictionaryList} from '../../api/api';
    import utils from '../../api/utils.js';
    export default {
        data() {
            return {
                formData: {
                    username: 'admin'
                    ,password: 'admin'
                    ,grant_type: 'password'
                }
            };
        },
        methods: {
            doSubmit:function () {
                var _this=this;
                getToken("username="+this.formData.username+"&password="+this.formData.password+"&grant_type="+this.formData.grant_type)
                    .then(res=>{
                        console.log(res);
                        localStorage.token = res.data.access_token;
                        localStorage.retoken = res.data.refresh_token;
                        // 初始化api管理的权限信息  用ajax同步请求 每次登入获取权限信息
                        _this.$jq.ajax({
                            type : "POST", //提交方式
                            url : zuulHost+"/uaa/roleApiPermission/searchByRole?access_token="+localStorage.token,//路径
                            data : "{}",
                            contentType: "application/json", // 请求数据格式(json)
                            async: false, // 同步
                            datatype: "json", // 返回数据格式(json)
                            headers: {"Content-Type":"application/json"},
                            success : function(result) {//返回数据根据结果进行相应的处理
                                if ( result.code === "S100101" ) {
                                    let json = JSON.stringify(result.data);
                                    localStorage.apiPermission = json;
                                    console.log(json);
                                } else {
                                    console.log("api权限获取失败！");
                                }
                            }
                        });
                        // 初始化字典
                        if(!sessionStorage.allDictData||sessionStorage.allDictData=='[]'||utils.isEmpty(JSON.parse(sessionStorage.allDictData))){
                            let params2 = {
                                data:{isPageAble: false},
                                access_token:"?access_token="+localStorage.token
                            }
                            initDictionaryList(params2).then(res=>{
                                sessionStorage.allDictData = JSON.stringify(res.data.data.list) ;
                            }).catch(err=>{
                                console.log(err);
                            });
                        }
                        _this.$router.go(-1);
                        // 这种设置 就算延迟执行，也不确定在延迟时间内，上面的异步请求是否已经完成，所以上面最好用同步请求获取数据
                        // setTimeout(function(){
                        //     _this.$router.go(-1);  // 登入时返回最后一次的路由  延迟执行
                        // },1000);
                    })
                    .catch(err=>{
                        console.log(err);
                    });
            }

        },
        mounted () {
            console.log(zuulHost+uamServer+clientID+authHost+tokenUri);
        }
    }
</script>

